package com.example.im.ui.activity

import android.content.Intent
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.example.im.R
import com.example.im.callback.OnClickRoomListener
import com.example.im.ui.fragment.RoomCentreFragment
import com.example.im.ui.fragment.RoomCreateFragment
import com.example.im.ui.fragment.RoomInviteFragment
import com.example.im.bean.UserRoom
import com.example.im.common.Constants
import com.example.im.mvp.presenter.RoomPresenter
import com.example.im.mvp.view.IRoomView
import com.example.im.ui.abstracts.AbstractBaseActivity
import com.example.im.ui.fragment.RoomOwnerFragment
import kotlinx.android.synthetic.main.include_toolbar.*

class RoomActivity : AbstractBaseActivity<RoomPresenter>(), IRoomView, OnClickRoomListener {

    private var userRoom: UserRoom? = null

    private var mOnToRoomCentreFragment: OnToRoomCentreFragment? = null

    interface OnToRoomCentreFragment {
        fun onRoomOwners(owners: MutableList<UserRoom>)
    }

    override val layoutId: Int
        get() = R.layout.activity_room

    override fun onAttachFragment(fragment: Fragment) {
        super.onAttachFragment(fragment)
        when (fragment) {
            is RoomCentreFragment -> {
                tv_toolbar_title.text = "群聊信息"
                userRoom = fragment.arguments?.getSerializable(Constants.BUNDLE_T0_USER_ROOM_DATA)
                        as UserRoom
                mOnToRoomCentreFragment = fragment
            }
            is RoomCreateFragment -> tv_toolbar_title.text = "创建群聊"
            is RoomInviteFragment -> tv_toolbar_title.text = "邀请好友"
            is RoomOwnerFragment -> tv_toolbar_title.text = "群聊成员"
        }
    }

    override fun initView() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        val serializable = intent.getSerializableExtra(Constants.BUNDLE_T0_USER_ROOM_DATA)
        val transaction = supportFragmentManager.beginTransaction()
        when (intent.getIntExtra(Constants.BUNDLE_T0_USER_ROOM_POINT, 0)) {
            Constants.BUNDLE_T0_ROOM_POINT_1 -> {
                if (serializable is UserRoom) transaction.replace(
                    R.id.frame_layout, RoomCentreFragment.newInstance(serializable)
                )
            }
            Constants.BUNDLE_T0_ROOM_POINT_2 -> {
                transaction.replace(R.id.frame_layout, RoomCreateFragment.newInstance())
            }
        }
        transaction.commit()
    }

    override fun initPresenter() {
        mPresenter = RoomPresenter(this, this)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
            android.R.id.home -> {
                onBackPressed()
            }
        }
        return super.onOptionsItemSelected(item)
    }


    /**
     * 群聊信息页面加载完成调用并获取群聊成员
     * */
    override fun onInitAffiliate(userRoom: UserRoom) {
        mPresenter!!.roomOwners(userRoom)
    }

    /**
     * 获取群聊成员成功调用
     * */
    override fun onRoomUserSuccess(userRooms: MutableList<UserRoom>) {
        mOnToRoomCentreFragment!!.onRoomOwners(userRooms)
    }




    /**
     * 群创建-点击事件
     * */
    override fun onClickCreate(view: View, name: String) {
        mPresenter!!.roomCreate(name)
    }

    /**
     * 群创建成功调用
     * */
    override fun onRoomCreateSuccess(userRoom: UserRoom) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.replace(R.id.frame_layout, RoomCentreFragment.newInstance(userRoom))
        transaction.commit()
    }




    /**
     * 群邀请好友-点击事件
     * */
    override fun onClickInvite(view: View, userRooms: List<UserRoom>) {
        mPresenter!!.roomInvite(userRoom!!.account!!, userRooms)
    }

    /**
     * 邀请好友成功调用
     * */
    override fun onRoomInviteSuccess() {
        onBackPressed()
        mPresenter!!.roomOwners(userRoom!!)
    }



    /**
     * 移除群成员-点击事件
     * */
    override fun onClickUserKick(view: View, userRooms: ArrayList<UserRoom>) {
        mPresenter!!.roomKick(userRoom!!.account!!, userRooms)
    }

    /**
     * 移除群成员成功调用
     * */
    override fun onRoomKickSuccess() {
        onBackPressed()
        mPresenter!!.roomOwners(userRoom!!)
    }



    /**
     * 群全部成员-点击事件
     * */
    override fun onClickUserTotal(view: View, userRooms: ArrayList<UserRoom>) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(R.id.frame_layout, RoomOwnerFragment.newInstance(userRooms))
        transaction.addToBackStack(null)
        transaction.commit()
    }

    /**
     * 进入邀请好友界面-点击事件
     * */
    override fun onClickUserAdd(view: View, userRooms: List<UserRoom>) {
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(
            R.id.frame_layout,
            RoomInviteFragment.newInstance(userRooms as ArrayList<UserRoom>)
        )
        transaction.addToBackStack(null)
        transaction.commit()
    }

    /**
     * 发送群聊天-点击事件
     * */
    override fun onClickSendChat(view: View, userRoom: UserRoom) {
//        val intent = Intent(this, ChatActivity::class.java)
//        intent.putExtra(Constants.BUNDLE_T0_CHAT_DATA, room)
//        startActivity(intent)
//        this.finish()
        val intent = Intent(this, ChatActivity2::class.java)
        intent.putExtra(Constants.BUNDLE_T0_CHAT_DATA, userRoom)
        startActivity(intent)
        finish()
    }

    /**
     * 删除聊天数据-点击事件
     * */
    override fun onClickClearChat(view: View, userRoom: UserRoom) {
        val dialog = AlertDialog.Builder(this)
            .setTitle(userRoom.nickname)
            .setMessage("是否清空聊天记录")
            .setPositiveButton("确定") { _, _ ->
                mPresenter!!.roomrDeleteChat(userRoom)
            }
            .setNegativeButton("取消", null)
        dialog.show()
    }

    /**
     * 删除数据并退出群-点击事件
     * */
    override fun onClickClearExit(view: View, userRoom: UserRoom) {
        val dialog = AlertDialog.Builder(this)
            .setTitle(userRoom.nickname)
            .setMessage("是否退出该群并删除聊天记录")
            .setPositiveButton("确定") { _, _ ->
                mPresenter!!.roomLeave(userRoom)
            }
            .setNegativeButton("取消", null)
        dialog.show()
    }


    /**
     * 退群成功调用
     * */
    override fun onRoomLeaveSuccess() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        this.finish()
    }


}